﻿using EquipSecs300;
using SecsEquip;
using SecsEquip300;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EquipSecs300
{
    public class GEM_Recipe : SecsEquipEx
    {
        string recipePath = null;
        bool PPIsFolder;
        bool PPAllowHostDowmload;
        private SecsParameter secsParameter;
        public Action EapDownLoadRecipe;
        public Action EapDownDeleteRecipe;
        public bool IsEapDownLoadRecipe { get;set; }
        public bool IsEapDeleteRecipe { get; set; }

        // 程式路径
        //string m_pRecipePath = null;
        public GEM_Recipe(CSecsEquip300 m_pSecs, SecsEquipHelper secsEquipHelper)
        {
            this.m_pSecs = m_pSecs;
            this.SecsEquipHelper = secsEquipHelper;
            this.secsParameter = SecsParameterHelper.SecsParam;
        }

        public override void UpdateData()
        {
            recipePath = SecsParameterHelper.SecsParam.ProcessProgramPath;
            PPIsFolder = SecsParameterHelper.SecsParam.PPIsFolder;
            PPAllowHostDowmload = SecsParameterHelper.SecsParam.PPAllowHostDowmload;

            m_pSecs.SetRecipeDirectory(recipePath);
            m_pSecs.PPIsFolderDir(PPIsFolder);
            m_pSecs.PPAllowHostDowmload(PPAllowHostDowmload);

            m_pSecs.m_pPPChangeNotifyProc += OnPPChangeNotifyEvent;
        }

        [Description("PP发生改变")]
        void OnPPChangeNotifyEvent(object sender, PPChangeNotifyEventArgs e)
        {
            switch (e.nChangeEvent)
            {
                case PP_CHANGE_TYPE.ePPLoad:
                    {
                        LogMsg($"SUCC: Recipe: {e.ppid} 上传成功!");
                    }
                    break;
                case PP_CHANGE_TYPE.ePPDownload:
                    {
                        EapDownLoadRecipe?.Invoke();
                        IsEapDownLoadRecipe = true;
                        LogMsg($"SUCC: Recipe: {e.ppid} 下载成功!");
                    }
                    break;
                case PP_CHANGE_TYPE.ePPDelete:
                    {
                        EapDownDeleteRecipe?.Invoke();
                        IsEapDeleteRecipe = true;
                        LogMsg($"SUCC: Recipe: {e.ppid} 删除成功!");
                    }
                    break;
            }
        }

        /// <summary>
        /// 获取路径下全部Recipe名
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public List<string> GetDirAllFile(string ProcessProgramPath, bool PPIsFolder)
        {
            List<string> listFile;
            if (PPIsFolder)
            {

                listFile = UtilitiesFile.GetAllFolderName(ProcessProgramPath);
            }
            else
            {
                listFile = UtilitiesFile.GetDirAllFile(ProcessProgramPath);
            }
            return listFile;

        }

        public void Upload(string ppid)
        {
            RcResult rs = m_pSecs.ProcessProgramUpload(ppid);
            ShowResult(rs);
        }

        public void Download(string ppid)
        {
            RcResult rs = m_pSecs.ProcessProgramDownload(ppid);
            ShowResult(rs);
        }

        void ShowResult(RcResult rs)
        {
            if (rs != 0)
            {
                MessageBox.Show(rs.toString());
            }
        }
    }
}
